<?php


class Meta extends FrameWork_Helper
{
    protected $_metabox_properties = array();

    protected $_metabox;

    protected $_metavalues = array();

    protected $_object_name = '';

    protected $_nonce = '';

    protected $_form_elements = array();

    public function __construct(WP_Form $metabox = null, $args = array())
    {
        if (!is_null($metabox)) {
            global $post;

            if (empty($args)) {
                $error = new WP_Error('wp_meta_error', 'Arguments for WP_Meta must not be empty!');
                wp_die($error->get_error_message());
            }

            $this->_object_name        = $args['name'];
            $this->_nonce              = $args['nonce'];
            $this->_metavalues         = get_post_meta($post->ID, $key, true);
            $this->_metabox_properties = $metabox->_form_properties;            

            if (empty($args['class'])) {
                $this->_form_elements      = $metabox->_form_elements;
                add_action('add_meta_boxes', array(&$this, 'pre_output_metaboxes'));
            } else {
                $this->pre_output_metaboxes($metabox->_form_elements);
            }            
        }

    }
    
    public function init()
    {
        add_action('save_post', array(&$this, 'save_post'));
    }
    
    private function _set_metabox_properties($props = array())
    {
        $args = array_merge($this->_metabox_properties, $props);
        extract($args);
       
        $args = array(
            'id'            => $id,
            'title'         => $title,
            'callback'      => $callback,
            'page'          => $this->_object_name,
            'context'       => $context,
            'priority'      => $priority,
            'callback_args' => $callback_args,
        );

        $this->_metabox_properties = $args;
        
        return $this->_metabox_properties;
    }
    
    
    public function pre_output_metaboxes($elements = null)
    {
        global $post;

        if (!is_array($elements) || $elements == null) {
            $elements = $this->_form_elements;
        }
        
        $properties             = $this->_metabox_properties;
        $properties['callback'] = empty($properties['callback'])
                                        ? array(&$this, 'output_metaboxes')
                                        : $properties['callback'];
        
        $description            = empty($properties['description'])
                                        ? ''
                                        : $properties['description'] ;

        
        $meta_key = "{$properties['id']}_meta";
        $data     = get_post_meta($post->ID, $meta_key, true);

        $html = '';
        if (!empty($elements)) {
            foreach($elements as $element) {

                $valueName  = $element->getName();
                $valueToken = "%{$valueName}%";

                $value = empty($data[$valueName]) 
                       ? ''
                       : $data[$valueName];

                $html .= str_replace($valueToken, $value, $element->getHtml());
            }

            $properties['callback_args'] = array(
                'html'        => $html,
                'description' => $description,
            );
        } else {
            $properties['callback_args'] = '';
        }
        
        $params = $this->_set_metabox_properties($properties);

        extract($params);
        add_meta_box(
            $id,
            $title,
            $callback,
            $page,
            $context,
            $priority,
            $callback_args
        );

        return $this;
        
    }

    public function output_metaboxes($post, $params)
    {
        
        // need to echo out hidden nonce element
        $nonce_elem = "<input type=\"hidden\""
                    . " name=\"{$this->_object_name}_nonce\""
                    . " value=\"$this->_nonce\""
                    . " />";
                    
        echo $nonce_elem;
        echo $params['args']['html'];

    }

    public function set_objectname($name)
    {
        
        $this->_object_name = $name;

        return $this;
    }

    public function set_nonce($nonce)
    {
        $this->_nonce = $nonce;

        return $this;
    }

    public function save_post($post_id)
    {
        //die('saved!');
    }

}
